package com.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bean.ResponseBean;
import com.pojo.Card;
import com.pojo.Dictionary;
import com.pojo.ErrorCard;
import com.pojo.UserInfo;
import com.service.CardService;
import com.service.DictionaryService;
import com.service.ErrorCardService;
import com.service.UserInfoService;
import com.util.StringUtil;
import com.vo.CardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/card")
public class CardAction {
    @Autowired
    private CardService cardService;

    @Autowired
    private ErrorCardService errorCardService ;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private UserInfoService staffService;

    @RequestMapping("/getCard")
    public ResponseBean getCard(int page, int limit, String xuliehao, @RequestParam(required = false)String status, HttpServletRequest request){

        String queryStr = null;
        if (StringUtil.isNull(xuliehao)) {
            xuliehao = null;
        }
        if (StringUtil.isNull(status)) {
            status = null;
        }
        if (page == 1) {
            page = 0;
        } else {
            page = (page * limit) - limit;
        }
        int count = cardService.getCardCount(xuliehao, status, queryStr);
        List<CardVo> cardList = cardService.getCardList(page, limit, xuliehao, status, queryStr);
        return new ResponseBean(0, count, "成功", cardList);
    }

    @RequestMapping("/get3Des")
    public ResponseBean get3Des(String random, String xuliehao, HttpServletRequest request) {
        String des = cardService.get3Des(random,xuliehao);
        if ("" == des) {
            return new ResponseBean(500, "序列号不存在！", null);
        }
        return new ResponseBean(200,1,"获取成功！",des);
    }

    @RequestMapping("/getCardStatusDic")
    public ResponseBean getCardStatusDic(HttpServletRequest request) {
        List<Dictionary> cardStatusList = dictionaryService.getDicByType("card_status");
        return new ResponseBean(0,1,"",cardStatusList);
    }

    /**
     * 挂失补卡
     */
    @RequestMapping("/cardGs")
    @ResponseBody
    public ResponseBean cardGs(String id, Integer status) {
        Card card = new Card();
        if(status==1){
            card.setXuliehao(id);
            card.setStatus(3);
        }else{
            card.setXuliehao(id);
            card.setStatus(1);
            card.setLuRuCreateTime(new Date());
        }
        Card c2 = cardService.getCardByCardNo(id);
        if (c2 != null) {
            UserInfo staff = staffService.getById(c2.getStaffId());
            card.setPin1(staff.getPin());
        }
        UpdateWrapper<Card> cardUpdateWrapper = new UpdateWrapper<Card>();
        cardUpdateWrapper.eq("xuliehao",id);
        return new ResponseBean(0,1,"修改卡状态",cardService.update(card,cardUpdateWrapper));
    }

    /**
     * 修改卡状态
     */
    @RequestMapping("/updStatus")
    public ResponseBean updStatus(@RequestBody Card card) {
        if(card.getStatus() != 1) {
            card.setSxTime(new Date());
        }
        card.setStatus(card.getStatus());
        UpdateWrapper<Card> cardUpdateWrapper = new UpdateWrapper<Card>();
        cardUpdateWrapper.eq("xuliehao",card.getXuliehao());
        return new ResponseBean(0,1,"修改卡状态",cardService.update(card,cardUpdateWrapper));
    }

    /**
     * 校验会员和卡
     */
    @RequestMapping("/validateCard")
    public ResponseBean validateCard(String cardNo) {
        UserInfo staff = staffService.getStaffByCardNo(cardNo);
        Card card = cardService.getCardByCardNo(cardNo);
        List<ErrorCard> errorCards = errorCardService.list(new QueryWrapper<ErrorCard>().eq("card_no" , cardNo)) ;
        if(errorCards.size() != 0) {
            return new ResponseBean(501 , "问题卡" , "") ;
        }
        if (card != null) {
            if (StringUtil.isNull(card.getMiyao())) {
                return new ResponseBean(500,1,"密钥丢失，废卡","密钥丢失，废卡");
            }
            UserInfo s = staffService.getById(card.getStaffId());
            if (s != null) {
                if (StringUtil.isNull(s.getCardNo()) || StringUtil.isNull(s.getPin())) {
                    return new ResponseBean(500,1,"pin密钥丢失需要重新写卡","pin密钥丢失需要重新写卡");
                }
            }
        }
        if (staff != null) {
            Card c = cardService.getCardByStaffId(staff.getId());
            if (null == c) {
                return new ResponseBean(500,1,"卡异常，废卡","卡异常，废卡");
            }
        }
        return new ResponseBean(200,1,"卡正常","卡正常");
    }

    @RequestMapping("bangdin")
    public ResponseBean bangdin(String cid , String uid){
        return null ;
    }
}